#include "adminform.h"
#include "ui_adminform.h"
#include "flatui.h"
#include <QMessageBox>
#include "db.h"
#include <QString>
#include <QDebug>

AdminForm::AdminForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AdminForm)
{
    ui->setupUi(this);
    this->initForm();
}

AdminForm::~AdminForm()
{
    delete ui;
}

void AdminForm::initForm()
{
    this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
    FlatUI::setPushButtonQss(ui->pushButton_Add);
    FlatUI::setPushButtonQss(ui->pushButton_Addc);
    FlatUI::setPushButtonQss(ui->pushButton_Close);
    FlatUI::setPushButtonQss(ui->pushButton_Deletep);
    FlatUI::setPushButtonQss(ui->pushButton_Deletec);
    FlatUI::setPushButtonQss(ui->pushButton_Addadmin);
    FlatUI::setPushButtonQss(ui->pushButton_Change);
    FlatUI::setPushButtonQss(ui->pushButton_Deleteadmin);

}

void AdminForm::initIfm(int id, const QString &uname, int level)
{
    this->id = id;
    this->uname = uname;
    this-> level = level;
    ui->label_ID->setText(QString::number(this->id));
    ui->label_Uname->setText(this->uname);
    if(this->level == 1)
        ui->label_Level->setText("admin");
    else if(this->level == 2)
        ui->label_Level->setText(" superadmin");
    else
        ui->label_Level->setText("no power");
    ui->comboBox_category->addItems(Db::getCategoryName());
    if(this->level == 2) this->resize(1181,475);
}

void AdminForm::on_pushButton_Addc_clicked()
{
    if (this->level < 1 )
    {
        QString dlgTitle="fail";
        QString strInfo="low level";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_cname->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_cdesc->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (Db::insertCategory(ui->lineEdit_cname->text(),ui->lineEdit_cdesc->text()))
    {
        QString dlgTitle="insert";
        QString strInfo="successful";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        ui->comboBox_category->clear();
        ui->comboBox_category->addItems(Db::getCategoryName());
    }
    else
    {
        QString dlgTitle="insert";
        QString strInfo="fail";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
}

void AdminForm::on_pushButton_Add_clicked()
{
    if (this->level < 1 )
    {
        QString dlgTitle="fail";
        QString strInfo="low level";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_pname->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_pdesc->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_price->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_positon->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_paddress->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    int cno = Db::getCategoryID(ui->comboBox_category->currentText());
    if(Db::insertProduct(ui->lineEdit_pname->text(), ui->lineEdit_paddress->text(), ui->lineEdit_pdesc->text(), ui->lineEdit_positon->text(), ui->lineEdit_price->text().toDouble(), cno))
    {
        QString dlgTitle="insert";
        QString strInfo="successful";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
    else
    {
        QString dlgTitle="insert";
        QString strInfo="fail";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
}

void AdminForm::on_pushButton_Close_clicked()
{
    this->close();
}

void AdminForm::on_pushButton_Deletep_clicked()
{
    if (this->level < 1 )
    {
        QString dlgTitle="fail";
        QString strInfo="low level";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_pnamed->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if(Db::deleteProduct(ui->lineEdit_pnamed->text()))
    {
        QString dlgTitle="delete";
        QString strInfo="successful";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
    else
    {
        QString dlgTitle="delete";
        QString strInfo="fail";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
}

void AdminForm::on_pushButton_Deletec_clicked()
{
    if (this->level < 1 )
    {
        QString dlgTitle="fail";
        QString strInfo="low level";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_cnamed->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if(Db::deleteCategory(ui->lineEdit_cnamed->text()))
    {
        QString dlgTitle="delete";
        QString strInfo="successful";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        ui->comboBox_category->clear();
        ui->comboBox_category->addItems(Db::getCategoryName());
    }
    else
    {
        QString dlgTitle="delete";
        QString strInfo="fail";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
}


void AdminForm::on_pushButton_Change_clicked()
{
    if (this->level < 1 )
    {
        QString dlgTitle="fail";
        QString strInfo="low level";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_pnamec->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_oldpwd->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_newpwd->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if(Db::changePassword(ui->lineEdit_pnamec->text(), ui->lineEdit_oldpwd->text(), ui->lineEdit_newpwd->text()))
    {
        QString dlgTitle="change";
        QString strInfo="successful";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
    else
    {
        QString dlgTitle="change";
        QString strInfo="fail";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
}

void AdminForm::on_pushButton_Addadmin_clicked()
{
    if (this->level < 2 )
    {
        QString dlgTitle="fail";
        QString strInfo="low level";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_pwd->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_username->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if(Db::insertUser(ui->lineEdit_username->text(),ui->lineEdit_pwd->text()))
    {
        QString dlgTitle="insert";
        QString strInfo="successful";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
    else
    {
        QString dlgTitle="insert";
        QString strInfo="fail";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
}

void AdminForm::on_pushButton_Deleteadmin_clicked()
{
    if (this->level < 2 )
    {
        QString dlgTitle="fail";
        QString strInfo="low level";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if (ui->lineEdit_usernamed->text() == "")
    {
        QString dlgTitle="fail";
        QString strInfo="empty";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        return;
    }
    if(Db::deleteUser(ui->lineEdit_usernamed->text()))
    {
        QString dlgTitle="delete";
        QString strInfo="successful";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
    else
    {
        QString dlgTitle="delete";
        QString strInfo="fail";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
}
